<?
$tbArr = array();
$getArr = $_GET; unset($_GET);

function getDateForMysqlDayField() {
	$date = getDate();
	foreach($date as $item=>$value) {
		if ($value < 10)
		$date[$item] = "0".$value;
	}
	return $date['year']."-".$date['mon']."-".$date['mday'];
}

// CSV데이터 가공
function csvOutFunc(&$value,$key) {
	$value = mb_convert_encoding($value,"EUC-KR","UTF-8");
	$value = str_replace("'", '"', $value);
	$value = str_replace(',', '', $value);
	$value = str_replace(array("\r\n", "\n", "\r"), " ", $value);
	##$value = '"'.$value.'"';
}

header('Pragma: private');
header('Cache-control: private, must-revalidate');
header('Content-type: application/octet-stream');
header('Content-Length: ' . strlen($csvData));
header('Content-Disposition: attachment; filename="'.$getArr['tp'].'-'.getDateForMysqlDayField().'.csv"');
header('Expires: Mon, 3 Jun 2005 10:00:00 GMT');
header('Content-Transfer-Encoding: binary');


## include config & db info##
include $_SERVER['DOCUMENT_ROOT']."/pos/lib/list.php";	
## include SQL header ##
include $_SERVER['DOCUMENT_ROOT']."/pos/lib/array_sql.php";

unset($_POST); $tbArr = array();
	

## csv 다운로드 페이지 ##
## 넘어온 get값으로 csv를 다운로드 할 수 있게 해준다
switch($getArr['tp']) {
	
	
	case 'customer' 	: 	## 고객의 CSV정보 ~>##
			
	
		$tbArr['Title'] = 
			array('No.','Name','Address','Telephone','Phone','Fax','Mailing');
			
		$query = sprintf(posSql('customer_info_view'), '', 'asc'); //select * from uatp_customer 
		$result	= DBquery($query);$No=1;		
		
		for($i=1; $list = mysql_fetch_array($result); $i++){	
			
			//$list['name'] = $list['name'];
			//$list['addr1'];		
			
			$tbArr['Fld'][] 	= 
				array($No++,$list['name'],$list['addr1'],$list['tel'],
				$list['phone'],$list['fax'],$list['mailingck']);
				
		} 	//$tbArr['fileName'] = 'member_parson.csv';
		
	break; 				## 일반 개인회원의 CSV정보 <~##
	
}
mysql_free_result($result);

array_walk($tbArr['Title'],"csvOutFunc");
$csvData = join(",",$tbArr['Title'])."\n";

if(is_array($tbArr['Fld'])) foreach($tbArr['Fld'] as $v) {
	array_walk($v,"csvOutFunc"); $csvData.= join(",",$v)."\n";
} 	$filename = $tbArr['fileName'];



die($csvData);
		

?>